package com.sxj.PDMS.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxj.PDMS.common.ErrorCode;
import com.sxj.PDMS.exception.BusinessException;
import com.sxj.PDMS.model.dto.student.YusRecordRequest;
import com.sxj.PDMS.model.entity.Student;
import com.sxj.PDMS.model.entity.YusIncompleteRecord;
import com.sxj.PDMS.model.entity.YusRecordSheet;
import com.sxj.PDMS.model.vo.PagingQueryResponse;
import com.sxj.PDMS.service.YusIncompleteRecordService;
import com.sxj.PDMS.mapper.YusIncompleteRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
* @author 503c'j
* @description 针对表【yus_incomplete_record(青年大学习未完成记录表)】的数据库操作Service实现
* @createDate 2024-06-18 16:45:09
*/
@Service
@Slf4j
public class YusIncompleteRecordServiceImpl extends ServiceImpl<YusIncompleteRecordMapper, YusIncompleteRecord>
    implements YusIncompleteRecordService{
    @Autowired
    private YusIncompleteRecordMapper yusIncompleteRecordMapper;
    @Override
    public Boolean yusRecordUpload(YusRecordRequest yusRecordRequest) {
        YusIncompleteRecord yusIncompleteRecord = new YusIncompleteRecord();

        yusIncompleteRecord.setStudentNumber(yusRecordRequest.getStudentNumber());
        yusIncompleteRecord.setName(yusRecordRequest.getName());
        yusIncompleteRecord.setGrade(yusRecordRequest.getGrade());

        boolean saved = this.save(yusIncompleteRecord);
        return saved;
    }

    @Override
    public PagingQueryResponse<YusIncompleteRecord> selectAll(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<YusIncompleteRecord> yusRecordSheetList = yusIncompleteRecordMapper.selectAll(start, pageSize);
        Long total = yusIncompleteRecordMapper.selectTotal();
        PagingQueryResponse<YusIncompleteRecord> pagingQueryResponse = new PagingQueryResponse<>();

        pagingQueryResponse.setRows(yusRecordSheetList);

        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }

    @Override
    public PagingQueryResponse<YusIncompleteRecord> yusIncompleteRecordSelect(Integer page, Integer pageSize, YusRecordRequest yusRecordRequest) {
        Integer start = (page - 1) * pageSize;
        String name = yusRecordRequest.getName();
        String studentNumber = yusRecordRequest.getStudentNumber();
        String grade = yusRecordRequest.getGrade();

        List<YusIncompleteRecord> yusRecordSheetList = yusIncompleteRecordMapper.selectByNameOrGradeOrStudentNumber(start, pageSize, name,grade,studentNumber);
        Long total = yusIncompleteRecordMapper.selectTotalByNameOrGradeOrStudentNumber(name,grade,studentNumber);

        PagingQueryResponse<YusIncompleteRecord> pagingQueryResponse = new PagingQueryResponse<>();
        pagingQueryResponse.setRows(yusRecordSheetList);
        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }

    @Override
    public void selectAllStudentAndDownload(YusRecordRequest yusRecordRequest, HttpServletResponse response) {
        String name = yusRecordRequest.getName();
        String grade = yusRecordRequest.getGrade();
        String studentNumber = yusRecordRequest.getStudentNumber();

        List<YusIncompleteRecord> yusIncompleteRecordList = yusIncompleteRecordMapper.selectAllStudentAndDownload(name,grade,studentNumber);
        if (yusIncompleteRecordList == null || yusIncompleteRecordList.isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "没有符合条件的学生");
        }
        // 2.通过POI将数据写入template/未完成学生信息表.xlsx文件
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream("template/未完成学生信息表.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(stream);

            // 填充数据
            for (int i = 0; i < yusIncompleteRecordList.size(); i++) {
                XSSFSheet sheet = excel.getSheetAt(0);
                // 获取第i+1行
                XSSFRow row = sheet.createRow(i + 2);
                YusIncompleteRecord yusIncompleteRecord = yusIncompleteRecordList.get(i);
                row.createCell(0).setCellValue(yusIncompleteRecord.getStudentNumber());
                row.createCell(1).setCellValue(yusIncompleteRecord.getName());
                row.createCell(2).setCellValue(yusIncompleteRecord.getGrade());
            }

            XSSFSheet sheet = excel.getSheetAt(0);
            XSSFRow row = sheet.createRow(yusIncompleteRecordList.size() + 2);
            row.createCell(0).setCellValue("下载时间:" + new Date().toString());


            // 3.通过输出流将文件下载到浏览器
            ServletOutputStream outputStream = response.getOutputStream();

            excel.write(outputStream);

            // 4.关闭资源
            excel.close();
            outputStream.close();

            log.info("未完成学生信息表.xlsx下载成功");

        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "文件下载失败");
        }
    }
}




